package controller;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import dao.SubjectDao;
import dao.impl.SubjectDaoImpl;
import service.SubjectService;
import service.impl.SubjectServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;

/**
 * @author: wyt
 * @date: 2022/12/18 17:12
 */
@WebServlet("/subjectDelete")
public class SubjectDeleteServlet extends HttpServlet {
    SubjectService subjectService = new SubjectServiceImpl();
    SubjectDao subjectDao=new SubjectDaoImpl();
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("application/json;charset=utf-8");

        //Json转换
        BufferedReader bufferedReader = new BufferedReader(request.getReader());
        String line = null;
        final StringBuilder json = new StringBuilder();
        while ((line = bufferedReader.readLine()) != null) {
            json.append(line);
        }
        JSONObject jsonObject = JSON.parseObject(json.toString());

        //
        Long sid=jsonObject.getLong("sid");
        Long uid=jsonObject.getLong("uid");
        subjectDao.delete(sid,uid);
        //执行方法存储后返回结果
        JSONObject message=new JSONObject();
        if(subjectService.delete(sid,uid)){
            message.put("status","1");
            message.put("Msg","删除成功");
        }else {
            message.put("status","0");
            message.put("Msg","删除失败");
        }
        response.getWriter().write(message.toString());
    }
}
